package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopShare;
import com.bdqn.service.ShopShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分享
 *
 * @author lilibo
 * @date 2021/10/01
 */
@Api(tags="分享接口")
@RestController
@RequestMapping("/shopshare")
public class ShopShareController {

    @Autowired
    private ShopShareService shopShareService;

    /**
     * 新增分享记录
     *
     * @param shopShare
     * @return R
     */
    @ApiOperation("新增分享记录")
    @WebLogger("新增分享记录")
    @PostMapping("/add")
    public R save(@ApiParam("分享对象") @RequestBody ShopShare shopShare) {
        return new R<>(shopShareService.save(shopShare));
    }

    /**
     * 修改分享记录
     *
     * @param shopShare
     * @return R
     */
    @ApiOperation("修改分享记录")
    @WebLogger("修改分享记录")
    @PostMapping("/update")
    public R update(@ApiParam("分享对象") @RequestBody ShopShare shopShare) {
        return new R<>(shopShareService.updateById(shopShare));
    }

    /**
     * 通过id删除一条分享记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条分享记录")
    @WebLogger("通过id删除一条分享记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("分享id") Integer id) {
        return new R<>(shopShareService.removeById(id));
    }

    /**
     * 通过多个id批量删除分享记录
     *
     * @param ids
     * @return R
     */
    @ApiOperation("通过多个id批量删除分享记录")
    @WebLogger("通过多个id批量删除分享记录")
    @PostMapping("/remove")
    public R removeByIds(@ApiParam("分享id集合") @RequestBody List<Integer> ids) {
        return new R<>(shopShareService.removeByIds(ids));
    }

    /**
     * 通过id查询单条分享记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条分享记录")
    @WebLogger("通过id查询单条分享记录")
    @GetMapping("/{id}")
    public R<ShopShare> getById(@ApiParam("分享id") @PathVariable Integer id) {
        return new R<>(shopShareService.getById(id));
    }

    /**
     * 通过分享信息查询分享记录
     *
     * @param shopShare 分享信息对象
     * @return R
     */
    @ApiOperation("通过分享信息查询分享记录")
    @WebLogger("通过分享信息查询分享记录")
    @GetMapping("/list")
    public R<List<ShopShare>> getList(@ApiParam("查询的分享对象") ShopShare shopShare) {
        return new R<>(shopShareService.getList(shopShare));
    }

    /**
     * 分页查询分享
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopShare 分享
     * @return
     */
    @ApiOperation("分页查询分享")
    @WebLogger("分页查询分享")
    @GetMapping("/page")
    public R<IPage<ShopShare>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的分享对象") ShopShare shopShare) {
        return new R<>(shopShareService.getPage(new Page<>(pageNo, pageSize), shopShare));
    }

    /**
     * 分页查询分享数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopShare 分享
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询分享数据")
    @WebLogger("分页查询分享数据")
    @GetMapping("/pagelist")
    public R<List<ShopShare>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的分享对象") ShopShare shopShare) {
        IPage<ShopShare> page = shopShareService.getPage(new Page<>(pageNo, pageSize), shopShare);
        return new R<>(page.getRecords(), page.getTotal());
    }

}
